<?php
/**
 * Created by PhpStorm.
 * User: taoyufang
 * Date: 17/12/06
 * Time: 16:52
 */

class TaskModel extends MY_Model{

    protected $table = 't_task';

    public function __construct(){
        parent::__construct();
    }

    /**
     * 获取奖励列表
     * name: getList
     * @param string $like
     * @param int $page
     * @param int $pagesize
     * @return array
     * @Author:taiyang
     */
    public function getTaskList($like = '',$page = 1, $pagesize = 30,$category=-1,$pid=-2){
        $sql="select * from ".$this->table." WHERE 1 ";
        if(!empty($like)){
            $sql .= " and (`name` like '%".$like."%' or `desc` like '%".$like."%' or `code` like '%".$like."%' ) ";
        }
        if($category>-1){
            $sql .= " and category = '".$category."' ";
        }
        if($pid>-2){
            $sql .= " and pid = '".$pid."' ";
        }
        //根据字段控制后台不显示在列表中，修改需要改数据表
        $sql.=" and admin_show = 1 ";
        $sql.=' order by category desc ';
        if($page != 0){ //等于0 不分页
            $index = ($page - 1) * $pagesize > 0 ? ($page - 1) * $pagesize : 0;
            $sql.= " limit ".$index.",".$pagesize;
        }
//        echo $sql;exit;
        return $this->query_sql_list($sql);
    }

    /**
     * 获取任务总数量
     * name: getListCnt
     * @param string $like
     * @return int
     * @Author:taiyang
     *
     */
    public function getListCnt($like = '',$category=-1){
        $sql="select count(*) from ".$this->table." WHERE 1 ";
        if(!empty($like)){
            $sql .= " and (`name` like '%".$like."%' or `desc` like '%".$like."%' or `code` like '%".$like."%' ) ";
        }
        if($category>-1){
            $sql .= " and category = '".$category."' ";
        }

        $sql.=" and admin_show = 1 ";

        return $this->query_sql_count($sql);
    }

    public function get_id($id){
        $sql='select * from '.$this->table.' where 1 ';
        if(!empty($id)){
            $sql.=" and id=".$id."";
        }else{
            return array();
        }
        $res=$this->query_sql_one($sql);
        $res['time']['h']=intval($res['when_long']%86400)/3600;
        $res['time']['d']=intval($res['when_long']/86400);
        return $res;
    }


    /*
    |--------------------------------------------------------------------------
    | 注：下方为实物奖品列表内容
    |                                               --------taiyang
    |--------------------------------------------------------------------------
    */

    /**
     * 获取实物奖励被领列表
     * name: getList
     * @param string $like
     * @param int $page
     * @param int $pagesize
     * @return array
     * @Author:taiyang
     */
    public function getGoodsList($like = '',$page = 1, $pagesize = 30){
        $sql="SELECT tp.id AS tp_id,tp.name,tp.category,tp.prize_data AS url,a.name as a_name,a.mobile as a_mobile,a.address as a_address,tp.img,utp.id AS utp_id,
              utp.task_id AS utp_task_id,utp.task_prize_id AS utp_prize_id,utp.receive_time,utp.uid,
              utp.validity_time,utp.status,utp.user_task_id,utp.address
              FROM t_task_prize tp
              INNER JOIN t_user_task_prize utp ON utp.task_prize_id=tp.id
              inner join t_user_address a on a.uid=utp.uid
              WHERE 1 AND tp.category = 4  ";
//        $sql="select * from ".$this->table." WHERE 1 ";
        if(!empty($like)){
            $sql .= " and tp.name like '%".$like."%' ";
        }
        if($page != 0){ //等于0 不分页
            $index = ($page - 1) * $pagesize > 0 ? ($page - 1) * $pagesize : 0;
            $sql.= " limit ".$index.",".$pagesize;
        }
        $res=$this->query_sql_list($sql);
        $res_x=array();
        foreach($res as $k=>$v){
            $res_x[$v['id']]=$v;
        }
        return $res_x;
    }

    /**
     * 获取实物被领取的奖品总数量
     * name: getListCnt
     * @param string $like
     * @return int
     * @Author:taiyang
     *
     */
    public function getGoodsListCnt($like = ''){
        $sql="SELECT count(*)
              FROM t_task_prize tp
              INNER JOIN t_user_task_prize utp ON utp.task_prize_id=tp.id
              WHERE 1 AND tp.category = 4 ";
        if(!empty($like)){
            $sql .= " and tp.name like '%".$like."%' ";
        }
        return $this->query_sql_count($sql);
    }

    public function getGoodsId($id){
        $sql='SELECT tp.id AS tp_id,tp.name,tp.category,tp.prize_data AS url,tp.img,utp.id AS utp_id,utp.uid,
              utp.task_id AS utp_task_id,utp.task_prize_id AS utp_prize_id,utp.receive_time,
              utp.validity_time,utp.status,utp.user_task_id,utp.address
              FROM t_task_prize tp
              INNER JOIN t_user_task_prize utp ON utp.task_prize_id=tp.id
              WHERE 1 AND tp.category = 4 ';
        if(!empty($id)){
            $sql.=" and utp.id=".$id."";
        }else{
            return array();
        }
        return $this->query_sql_one($sql);
    }


    public function get_code_title(){
        $list=$this->getList('',1,100);
        $data=array();
        foreach ($list as $k=>$v){
            $data[$v['code']]=$v['name'];
        }
        return $data;
    }

}